const {baseService} =require('../../baseService')





const {check_title_content_bean} =require('../../../beans/impl/check_title_content_bean/check_title_content_bean')
const {check_title_content_model} =require('../../../dao/model/model')

/* 
1,修改导入的bean,和model,就是上面
2,把构造方法里的这些都改下

*/

/* 异常类 */
class check_title_contentServiceError extends Error {
    constructor(error,message='check_title_contentServiceError'){
        super()
        this.error = error
        this.message=message
        
    }
}


class check_title_contentService {

    static ins = new check_title_contentService()

    constructor(){
        
        this.bean_class = check_title_content_bean
        this.model = check_title_content_model
        this.expClass = check_title_contentServiceError 
        
    }


    static getIns (){
        if(this.ins){
            return this.ins
        }else{
            return new check_title_contentService()
        }
    }


    async  create(data){
        let bean_obj = new this.bean_class()
     
            let  result 
            for (const key1 in data) {
                for (const key2 in bean_obj) {
                    if(key1 == key2){
                        bean_obj[key2] = data[key1]
                    }
                }
            }
        
            try {
                result  =  await new this.model(bean_obj).save()
            } catch (error) {
                throw new this.expClass(error)
            }
             if(result){
                return result
             }else{
                throw  new this.expClass()
             }
    
      
        
    
        }
    
      async   findByIdAndRemove(data){
            let  result
            try {
                result = await  this.model.findByIdAndRemove(data.id)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    
        
        async findByIdAndUpdate(id,updateContent){
            let  result
            try {
                result = await  this.model.findByIdAndUpdate(id,updateContent)
            } catch (error) {
                throw new this.expClass(error)
            }
    
       
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    
        async queryAll(param1,param2){
            let  result
    
    
            if(arguments.length==0 ){
                try {
                    result = await  this.model.find()
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==1){
                try {
                    result = await  this.model.find().populate(param1)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==2){
                try {
                    result = await  this.model.find().populate(param1).populate(param2)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
    
            }
    
    
    
    
        }
    
        async findByIdAndPopulate(id,param1,param2){
            let  result
            if(arguments.length==1){
                try {
                    result = await  this.model.findById(id)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==2){
                try {
                    result = await  this.model.findById(id).populate(param1)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==3){
                try {
                    result = await  this.model.findById(id).populate(param1).populate(param2)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
    
            }
    
        }
    
    
    
        async findOne(obj){
            let  result
            try {
                result = await  this.model.findOne(obj)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                return null
             }
    
        }
    
        // findByObjAndPopulate
        async findByObj(obj,param1,param2){
            if(arguments.length==1){
                let  result
                try {
                    result = await  this.model.find(obj)
                } catch (error) {
                    throw new this.expClass(error)
                }
        
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==2){
         
                let  result
                try {
                    result = await  this.model.find(obj).populate(param1)
                } catch (error) {
                    throw new this.expClass(error)
                }
        
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==3){
                let  result
                try {
                    result = await  this.model.find(obj).populate(param1).populate(param2)
                } catch (error) {
                    throw new this.expClass(error)
                }
        
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }
    
    
        }
    
        async find(obj){
            let  result
            try {
                result = await  this.model.find(obj)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    
        async findAndPopulate(obj,param1,param2){
            let result
            if(arguments.length==1){
                try {
                    result = await  this.model.find(obj)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }else if(arguments.length==2){
                try {
                    result = await  this.model.find(obj).populate(param1)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
    
            }else if(arguments.length==3){
                try {
                    result = await  this.model.find(obj).populate(param1).populate(param2)
                } catch (error) {
                    throw new this.expClass(error)
                }
                if(result){
                    return result
                 }else{
                    throw  new this.expClass()
                 }
            }
        }
    
        async findByObjAndUpdate(obj,content,options){
            let result
            try {
                result = await  this.model.findOneAndUpdate(obj,content,options)
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    
    
        // 更新数组
        async findByObjAndUpdateArray(obj,content){
            let result
            try {
                result = await  this.model.findOneAndUpdate(obj,{$addToSet:content})
            } catch (error) {
                throw new this.expClass(error)
            }
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
    
    
        // 存在则更新,不存在则新增
        async findOneAndUpdate(conditions,update,options){
    
            let  result
    
            try {
                result = await  this.model.findOneAndUpdate(conditions,update,options)
            } catch (error) {
                throw new this.expClass(error)
            }
    
            if(result){
                return result
             }else{
                throw  new this.expClass()
             }
        }
}







module.exports={check_title_contentService}
